﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using GPSTrackingDBV2.Entities;
using GPSTrackingDBV2.Data;

#endregion

namespace GPSTrackingDBV2.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ClientsGroupsProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ClientsGroupsProviderBaseCore : EntityProviderBase<GPSTrackingDBV2.Entities.ClientsGroups, GPSTrackingDBV2.Entities.ClientsGroupsKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByClientIdFromClientsByClientsGroups
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by ClientID in the
		///		ClientsByClientsGroups table. Table ClientsGroups is related to table Clients
		///		through the (M:N) relationship defined in the ClientsByClientsGroups table.
		/// </summary>
		/// <param name="_clientId"></param>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByClientIdFromClientsByClientsGroups(System.Int32 _clientId)
		{
			int count = -1;
			return GetByClientIdFromClientsByClientsGroups(null,_clientId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.ClientsGroups objects from the datasource by ClientID in the
		///		ClientsByClientsGroups table. Table ClientsGroups is related to table Clients
		///		through the (M:N) relationship defined in the ClientsByClientsGroups table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_clientId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByClientIdFromClientsByClientsGroups(System.Int32 _clientId, int start, int pageLength)
		{
			int count = -1;
			return GetByClientIdFromClientsByClientsGroups(null, _clientId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by ClientID in the
		///		ClientsByClientsGroups table. Table ClientsGroups is related to table Clients
		///		through the (M:N) relationship defined in the ClientsByClientsGroups table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_clientId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByClientIdFromClientsByClientsGroups(TransactionManager transactionManager, System.Int32 _clientId)
		{
			int count = -1;
			return GetByClientIdFromClientsByClientsGroups(transactionManager, _clientId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by ClientID in the
		///		ClientsByClientsGroups table. Table ClientsGroups is related to table Clients
		///		through the (M:N) relationship defined in the ClientsByClientsGroups table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_clientId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByClientIdFromClientsByClientsGroups(TransactionManager transactionManager, System.Int32 _clientId,int start, int pageLength)
		{
			int count = -1;
			return GetByClientIdFromClientsByClientsGroups(transactionManager, _clientId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by ClientID in the
		///		ClientsByClientsGroups table. Table ClientsGroups is related to table Clients
		///		through the (M:N) relationship defined in the ClientsByClientsGroups table.
		/// </summary>
		/// <param name="_clientId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByClientIdFromClientsByClientsGroups(System.Int32 _clientId,int start, int pageLength, out int count)
		{
			
			return GetByClientIdFromClientsByClientsGroups(null, _clientId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets ClientsGroups objects from the datasource by ClientID in the
		///		ClientsByClientsGroups table. Table ClientsGroups is related to table Clients
		///		through the (M:N) relationship defined in the ClientsByClientsGroups table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_clientId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of ClientsGroups objects.</returns>
		public abstract TList<ClientsGroups> GetByClientIdFromClientsByClientsGroups(TransactionManager transactionManager,System.Int32 _clientId, int start, int pageLength, out int count);
		
		#endregion GetByClientIdFromClientsByClientsGroups
		
		#region GetByCompanyIdFromClientsGroupsByCompanies
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by CompanyID in the
		///		ClientsGroupsByCompanies table. Table ClientsGroups is related to table Companies
		///		through the (M:N) relationship defined in the ClientsGroupsByCompanies table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByCompanyIdFromClientsGroupsByCompanies(System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyIdFromClientsGroupsByCompanies(null,_companyId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.ClientsGroups objects from the datasource by CompanyID in the
		///		ClientsGroupsByCompanies table. Table ClientsGroups is related to table Companies
		///		through the (M:N) relationship defined in the ClientsGroupsByCompanies table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByCompanyIdFromClientsGroupsByCompanies(System.Int32 _companyId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdFromClientsGroupsByCompanies(null, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by CompanyID in the
		///		ClientsGroupsByCompanies table. Table ClientsGroups is related to table Companies
		///		through the (M:N) relationship defined in the ClientsGroupsByCompanies table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByCompanyIdFromClientsGroupsByCompanies(TransactionManager transactionManager, System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyIdFromClientsGroupsByCompanies(transactionManager, _companyId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by CompanyID in the
		///		ClientsGroupsByCompanies table. Table ClientsGroups is related to table Companies
		///		through the (M:N) relationship defined in the ClientsGroupsByCompanies table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByCompanyIdFromClientsGroupsByCompanies(TransactionManager transactionManager, System.Int32 _companyId,int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdFromClientsGroupsByCompanies(transactionManager, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets ClientsGroups objects from the datasource by CompanyID in the
		///		ClientsGroupsByCompanies table. Table ClientsGroups is related to table Companies
		///		through the (M:N) relationship defined in the ClientsGroupsByCompanies table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ClientsGroups objects.</returns>
		public TList<ClientsGroups> GetByCompanyIdFromClientsGroupsByCompanies(System.Int32 _companyId,int start, int pageLength, out int count)
		{
			
			return GetByCompanyIdFromClientsGroupsByCompanies(null, _companyId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets ClientsGroups objects from the datasource by CompanyID in the
		///		ClientsGroupsByCompanies table. Table ClientsGroups is related to table Companies
		///		through the (M:N) relationship defined in the ClientsGroupsByCompanies table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of ClientsGroups objects.</returns>
		public abstract TList<ClientsGroups> GetByCompanyIdFromClientsGroupsByCompanies(TransactionManager transactionManager,System.Int32 _companyId, int start, int pageLength, out int count);
		
		#endregion GetByCompanyIdFromClientsGroupsByCompanies
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ClientsGroupsKey key)
		{
			return Delete(transactionManager, key.ClientGroupId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_clientGroupId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _clientGroupId)
		{
			return Delete(null, _clientGroupId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_clientGroupId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _clientGroupId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override GPSTrackingDBV2.Entities.ClientsGroups Get(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ClientsGroupsKey key, int start, int pageLength)
		{
			return GetByClientGroupId(transactionManager, key.ClientGroupId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ClientsGroups index.
		/// </summary>
		/// <param name="_clientGroupId"></param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> class.</returns>
		public GPSTrackingDBV2.Entities.ClientsGroups GetByClientGroupId(System.Int32 _clientGroupId)
		{
			int count = -1;
			return GetByClientGroupId(null,_clientGroupId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ClientsGroups index.
		/// </summary>
		/// <param name="_clientGroupId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> class.</returns>
		public GPSTrackingDBV2.Entities.ClientsGroups GetByClientGroupId(System.Int32 _clientGroupId, int start, int pageLength)
		{
			int count = -1;
			return GetByClientGroupId(null, _clientGroupId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ClientsGroups index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_clientGroupId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> class.</returns>
		public GPSTrackingDBV2.Entities.ClientsGroups GetByClientGroupId(TransactionManager transactionManager, System.Int32 _clientGroupId)
		{
			int count = -1;
			return GetByClientGroupId(transactionManager, _clientGroupId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ClientsGroups index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_clientGroupId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> class.</returns>
		public GPSTrackingDBV2.Entities.ClientsGroups GetByClientGroupId(TransactionManager transactionManager, System.Int32 _clientGroupId, int start, int pageLength)
		{
			int count = -1;
			return GetByClientGroupId(transactionManager, _clientGroupId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ClientsGroups index.
		/// </summary>
		/// <param name="_clientGroupId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> class.</returns>
		public GPSTrackingDBV2.Entities.ClientsGroups GetByClientGroupId(System.Int32 _clientGroupId, int start, int pageLength, out int count)
		{
			return GetByClientGroupId(null, _clientGroupId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ClientsGroups index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_clientGroupId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> class.</returns>
		public abstract GPSTrackingDBV2.Entities.ClientsGroups GetByClientGroupId(TransactionManager transactionManager, System.Int32 _clientGroupId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;ClientsGroups&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;ClientsGroups&gt;"/></returns>
		public static TList<ClientsGroups> Fill(IDataReader reader, TList<ClientsGroups> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				GPSTrackingDBV2.Entities.ClientsGroups c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("ClientsGroups")
					.Append("|").Append((System.Int32)reader[((int)ClientsGroupsColumn.ClientGroupId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<ClientsGroups>(
					key.ToString(), // EntityTrackingKey
					"ClientsGroups",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new GPSTrackingDBV2.Entities.ClientsGroups();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.ClientGroupId = (System.Int32)reader[((int)ClientsGroupsColumn.ClientGroupId - 1)];
					c.OriginalClientGroupId = c.ClientGroupId;
					c.Name = (reader.IsDBNull(((int)ClientsGroupsColumn.Name - 1)))?null:(System.String)reader[((int)ClientsGroupsColumn.Name - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, GPSTrackingDBV2.Entities.ClientsGroups entity)
		{
			if (!reader.Read()) return;
			
			entity.ClientGroupId = (System.Int32)reader[((int)ClientsGroupsColumn.ClientGroupId - 1)];
			entity.OriginalClientGroupId = (System.Int32)reader["ClientGroupID"];
			entity.Name = (reader.IsDBNull(((int)ClientsGroupsColumn.Name - 1)))?null:(System.String)reader[((int)ClientsGroupsColumn.Name - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, GPSTrackingDBV2.Entities.ClientsGroups entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ClientGroupId = (System.Int32)dataRow["ClientGroupID"];
			entity.OriginalClientGroupId = (System.Int32)dataRow["ClientGroupID"];
			entity.Name = Convert.IsDBNull(dataRow["Name"]) ? null : (System.String)dataRow["Name"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.ClientsGroups"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.ClientsGroups Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ClientsGroups entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByClientGroupId methods when available
			
			#region CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Companies>|CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies", deepLoadType, innerList))
			{
				entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies = DataRepository.CompaniesProvider.GetByClientGroupIdFromClientsGroupsByCompanies(transactionManager, entity.ClientGroupId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies != null)
				{
					deepHandles.Add("CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Companies >) DataRepository.CompaniesProvider.DeepLoad,
						new object[] { transactionManager, entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region ClientIdClientsCollection_From_ClientsByClientsGroups
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Clients>|ClientIdClientsCollection_From_ClientsByClientsGroups", deepLoadType, innerList))
			{
				entity.ClientIdClientsCollection_From_ClientsByClientsGroups = DataRepository.ClientsProvider.GetByClientGroupIdFromClientsByClientsGroups(transactionManager, entity.ClientGroupId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ClientIdClientsCollection_From_ClientsByClientsGroups' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ClientIdClientsCollection_From_ClientsByClientsGroups != null)
				{
					deepHandles.Add("ClientIdClientsCollection_From_ClientsByClientsGroups",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Clients >) DataRepository.ClientsProvider.DeepLoad,
						new object[] { transactionManager, entity.ClientIdClientsCollection_From_ClientsByClientsGroups, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region ClientsGroupsByCompaniesCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ClientsGroupsByCompanies>|ClientsGroupsByCompaniesCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ClientsGroupsByCompaniesCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ClientsGroupsByCompaniesCollection = DataRepository.ClientsGroupsByCompaniesProvider.GetByClientGroupId(transactionManager, entity.ClientGroupId);

				if (deep && entity.ClientsGroupsByCompaniesCollection.Count > 0)
				{
					deepHandles.Add("ClientsGroupsByCompaniesCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ClientsGroupsByCompanies>) DataRepository.ClientsGroupsByCompaniesProvider.DeepLoad,
						new object[] { transactionManager, entity.ClientsGroupsByCompaniesCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ClientsByClientsGroupsCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ClientsByClientsGroups>|ClientsByClientsGroupsCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ClientsByClientsGroupsCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ClientsByClientsGroupsCollection = DataRepository.ClientsByClientsGroupsProvider.GetByClientGroupId(transactionManager, entity.ClientGroupId);

				if (deep && entity.ClientsByClientsGroupsCollection.Count > 0)
				{
					deepHandles.Add("ClientsByClientsGroupsCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ClientsByClientsGroups>) DataRepository.ClientsByClientsGroupsProvider.DeepLoad,
						new object[] { transactionManager, entity.ClientsByClientsGroupsCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the GPSTrackingDBV2.Entities.ClientsGroups object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">GPSTrackingDBV2.Entities.ClientsGroups instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.ClientsGroups Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ClientsGroups entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies>
			if (CanDeepSave(entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies, "List<Companies>|CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies", deepSaveType, innerList))
			{
				if (entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies.Count > 0 || entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies.DeletedItems.Count > 0)
				{
					DataRepository.CompaniesProvider.Save(transactionManager, entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies); 
					deepHandles.Add("CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Companies>) DataRepository.CompaniesProvider.DeepSave,
						new object[] { transactionManager, entity.CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 

			#region ClientIdClientsCollection_From_ClientsByClientsGroups>
			if (CanDeepSave(entity.ClientIdClientsCollection_From_ClientsByClientsGroups, "List<Clients>|ClientIdClientsCollection_From_ClientsByClientsGroups", deepSaveType, innerList))
			{
				if (entity.ClientIdClientsCollection_From_ClientsByClientsGroups.Count > 0 || entity.ClientIdClientsCollection_From_ClientsByClientsGroups.DeletedItems.Count > 0)
				{
					DataRepository.ClientsProvider.Save(transactionManager, entity.ClientIdClientsCollection_From_ClientsByClientsGroups); 
					deepHandles.Add("ClientIdClientsCollection_From_ClientsByClientsGroups",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Clients>) DataRepository.ClientsProvider.DeepSave,
						new object[] { transactionManager, entity.ClientIdClientsCollection_From_ClientsByClientsGroups, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<ClientsGroupsByCompanies>
				if (CanDeepSave(entity.ClientsGroupsByCompaniesCollection, "List<ClientsGroupsByCompanies>|ClientsGroupsByCompaniesCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ClientsGroupsByCompanies child in entity.ClientsGroupsByCompaniesCollection)
					{
						if(child.ClientGroupIdSource != null)
						{
								child.ClientGroupId = child.ClientGroupIdSource.ClientGroupId;
						}

						if(child.CompanyIdSource != null)
						{
								child.CompanyId = child.CompanyIdSource.CompanyId;
						}

					}

					if (entity.ClientsGroupsByCompaniesCollection.Count > 0 || entity.ClientsGroupsByCompaniesCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ClientsGroupsByCompaniesProvider.Save(transactionManager, entity.ClientsGroupsByCompaniesCollection);
						
						deepHandles.Add("ClientsGroupsByCompaniesCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ClientsGroupsByCompanies >) DataRepository.ClientsGroupsByCompaniesProvider.DeepSave,
							new object[] { transactionManager, entity.ClientsGroupsByCompaniesCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<ClientsByClientsGroups>
				if (CanDeepSave(entity.ClientsByClientsGroupsCollection, "List<ClientsByClientsGroups>|ClientsByClientsGroupsCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ClientsByClientsGroups child in entity.ClientsByClientsGroupsCollection)
					{
						if(child.ClientGroupIdSource != null)
						{
								child.ClientGroupId = child.ClientGroupIdSource.ClientGroupId;
						}

						if(child.ClientIdSource != null)
						{
								child.ClientId = child.ClientIdSource.ClientId;
						}

					}

					if (entity.ClientsByClientsGroupsCollection.Count > 0 || entity.ClientsByClientsGroupsCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ClientsByClientsGroupsProvider.Save(transactionManager, entity.ClientsByClientsGroupsCollection);
						
						deepHandles.Add("ClientsByClientsGroupsCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ClientsByClientsGroups >) DataRepository.ClientsByClientsGroupsProvider.DeepSave,
							new object[] { transactionManager, entity.ClientsByClientsGroupsCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ClientsGroupsChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>GPSTrackingDBV2.Entities.ClientsGroups</c>
	///</summary>
	public enum ClientsGroupsChildEntityTypes
	{

		///<summary>
		/// Collection of <c>ClientsGroups</c> as ManyToMany for CompaniesCollection_From_ClientsGroupsByCompanies
		///</summary>
		[ChildEntityType(typeof(TList<Companies>))]
		CompanyIdCompaniesCollection_From_ClientsGroupsByCompanies,

		///<summary>
		/// Collection of <c>ClientsGroups</c> as ManyToMany for ClientsCollection_From_ClientsByClientsGroups
		///</summary>
		[ChildEntityType(typeof(TList<Clients>))]
		ClientIdClientsCollection_From_ClientsByClientsGroups,

		///<summary>
		/// Collection of <c>ClientsGroups</c> as OneToMany for ClientsGroupsByCompaniesCollection
		///</summary>
		[ChildEntityType(typeof(TList<ClientsGroupsByCompanies>))]
		ClientsGroupsByCompaniesCollection,

		///<summary>
		/// Collection of <c>ClientsGroups</c> as OneToMany for ClientsByClientsGroupsCollection
		///</summary>
		[ChildEntityType(typeof(TList<ClientsByClientsGroups>))]
		ClientsByClientsGroupsCollection,
	}
	
	#endregion ClientsGroupsChildEntityTypes
	
	#region ClientsGroupsFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ClientsGroupsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ClientsGroups"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ClientsGroupsFilterBuilder : SqlFilterBuilder<ClientsGroupsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsFilterBuilder class.
		/// </summary>
		public ClientsGroupsFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ClientsGroupsFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ClientsGroupsFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ClientsGroupsFilterBuilder
	
	#region ClientsGroupsParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ClientsGroupsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ClientsGroups"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ClientsGroupsParameterBuilder : ParameterizedSqlFilterBuilder<ClientsGroupsColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsParameterBuilder class.
		/// </summary>
		public ClientsGroupsParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ClientsGroupsParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ClientsGroupsParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ClientsGroupsParameterBuilder
	
	#region ClientsGroupsSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ClientsGroupsColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ClientsGroups"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ClientsGroupsSortBuilder : SqlSortBuilder<ClientsGroupsColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ClientsGroupsSqlSortBuilder class.
		/// </summary>
		public ClientsGroupsSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ClientsGroupsSortBuilder
	
} // end namespace
